package com.aptech.DAO;

import com.aptech.domain.JPA.EventRequest;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

/**
 	* A data access object (DAO) providing persistence and search support for EventRequest entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.aptech.DAO.EventRequest
  * @author MyEclipse Persistence Tools 
 */

public class EventRequestDAO extends JpaDaoSupport  implements IEventRequestDAO{
	//property constants
	public static final String STATUS = "status";
	public static final String ADDRESS_ID = "addressId";






		/**
	 Perform an initial save of a previously unsaved EventRequest entity. 
	 All subsequent persist actions of this entity should use the #update() method.
	 This operation must be performed within the a database transaction context for the entity's data to be permanently saved to the persistence store, i.e., database. 
	 This method uses the {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist} operation.
	 	 <p>
	 User-managed Spring transaction example:
	 	 	 
	 * <pre> 
	 *   TransactionStatus txn = txManager.getTransaction(new DefaultTransactionDefinition());
	 *   EventRequestDAO.save(entity);
	 *   txManager.commit(txn);
	 * </pre>
	 @see <a href = "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring container-managed transaction examples</a>
	   @param entity EventRequest entity to persist
	  @throws RuntimeException when the operation fails
	 */
    public void save(EventRequest entity) {
    				logger.info("saving EventRequest instance");
	        try {
            getJpaTemplate().persist(entity);
            			logger.info("save successful");
	        } catch (RuntimeException re) {
        				logger.error("save failed", re);
	            throw re;
        }
    }
    
    /**
	 Delete a persistent EventRequest entity.
	  This operation must be performed 
	 within the a database transaction context for the entity's data to be
	 permanently deleted from the persistence store, i.e., database. 
	 This method uses the {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete} operation.
	 	 <p>
	 User-managed Spring transaction example:
	 	 	 
	 * <pre> 
	 *   TransactionStatus txn = txManager.getTransaction(new DefaultTransactionDefinition());
	 *   EventRequestDAO.delete(entity);
	 *   txManager.commit(txn);
	 *   entity = null;
	 * </pre>
	 @see <a href = "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring container-managed transaction examples</a>
	   @param entity EventRequest entity to delete
	 @throws RuntimeException when the operation fails
	 */
    public void delete(EventRequest entity) {
    				logger.info("deleting EventRequest instance");
	        try {
        	entity = getJpaTemplate().getReference(EventRequest.class, entity.getEventRequestId());
            getJpaTemplate().remove(entity);
            			logger.info("delete successful");
	        } catch (RuntimeException re) {
        				logger.error("delete failed", re);
	            throw re;
        }
    }
    
    /**
	 Persist a previously saved EventRequest entity and return it or a copy of it to the sender. 
	 A copy of the EventRequest entity parameter is returned when the JPA persistence mechanism has not previously been tracking the updated entity. 
	 This operation must be performed within the a database transaction context for the entity's data to be permanently saved to the persistence
	 store, i.e., database. This method uses the {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge} operation.
	 	 <p>
	 User-managed Spring transaction example:
	 	 	 
	 * <pre> 
	 *   TransactionStatus txn = txManager.getTransaction(new DefaultTransactionDefinition());
	 *   entity = EventRequestDAO.update(entity);
	 *   txManager.commit(txn);
	 * </pre>
	 @see <a href = "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring container-managed transaction examples</a>
	   @param entity EventRequest entity to update
	 @return EventRequest the persisted EventRequest entity instance, may not be the same
	 @throws RuntimeException if the operation fails
	 */
    public EventRequest update(EventRequest entity) {
    				logger.info("updating EventRequest instance");
	        try {
            EventRequest result = getJpaTemplate().merge(entity);
            			logger.info("update successful");
	            return result;
        } catch (RuntimeException re) {
        				logger.error("update failed", re);
	            throw re;
        }
    }
    
    public EventRequest findById( Integer id) {
    				logger.info("finding EventRequest instance with id: " + id);
	        try {
            EventRequest instance = getJpaTemplate().find(EventRequest.class, id);
            return instance;
        } catch (RuntimeException re) {
        				logger.error("find failed", re);
	            throw re;
        }
    }    
    

/**
	 * Find all EventRequest entities with a specific property value.  
	 
	  @param propertyName the name of the EventRequest property to query
	  @param value the property value to match
	  	  @param rowStartIdxAndCount Optional int varargs. rowStartIdxAndCount[0] specifies the  the row index in the query result-set to begin collecting the results. rowStartIdxAndCount[1] specifies the the maximum number of results to return.  
	  	  @return List<EventRequest> found by query
	 */
    @SuppressWarnings("unchecked")
    public List<EventRequest> findByProperty(String propertyName, final Object value
        , final int...rowStartIdxAndCount
        ) {
    				logger.info("finding EventRequest instance with property: " + propertyName + ", value: " + value);
			try {
			final String queryString = "select model from EventRequest model where model." 
			 						+ propertyName + "= :propertyValue";
						return getJpaTemplate().executeFind(new JpaCallback() {
				public Object doInJpa(EntityManager em) throws PersistenceException {
					Query query = em.createQuery(queryString);
					query.setParameter("propertyValue", value);
					if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {	
						int rowStartIdx = Math.max(0,rowStartIdxAndCount[0]);
						if (rowStartIdx > 0) {
							query.setFirstResult(rowStartIdx);
						}
		
						if (rowStartIdxAndCount.length > 1) {
					    	int rowCount = Math.max(0,rowStartIdxAndCount[1]);
					    	if (rowCount > 0) {
					    		query.setMaxResults(rowCount);    
					    	}
						}
					}										
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
						logger.error("find by property name failed", re);
				throw re;
		}
	}			
	public List<EventRequest> findByStatus(Object status
	, int...rowStartIdxAndCount
	) {
		return findByProperty(STATUS, status
	, rowStartIdxAndCount
		);
	}
	
	public List<EventRequest> findByAddressId(Object addressId
	, int...rowStartIdxAndCount
	) {
		return findByProperty(ADDRESS_ID, addressId
	, rowStartIdxAndCount
		);
	}
	
	
	/**
	 * Find all EventRequest entities.
	  	  @param rowStartIdxAndCount Optional int varargs. rowStartIdxAndCount[0] specifies the  the row index in the query result-set to begin collecting the results. rowStartIdxAndCount[1] specifies the the maximum count of results to return.  
	  	  @return List<EventRequest> all EventRequest entities
	 */
	@SuppressWarnings("unchecked")
	public List<EventRequest> findAll(
		final int...rowStartIdxAndCount
		) {
					logger.info("finding all EventRequest instances");
			try {
			final String queryString = "select model from EventRequest model";
						return getJpaTemplate().executeFind(new JpaCallback() {
				public Object doInJpa(EntityManager em) throws PersistenceException {
					Query query = em.createQuery(queryString);
					if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {	
						int rowStartIdx = Math.max(0,rowStartIdxAndCount[0]);
						if (rowStartIdx > 0) {
							query.setFirstResult(rowStartIdx);
						}
		
						if (rowStartIdxAndCount.length > 1) {
					    	int rowCount = Math.max(0,rowStartIdxAndCount[1]);
					    	if (rowCount > 0) {
					    		query.setMaxResults(rowCount);    
					    	}
						}
					}										
					return query.getResultList();
				}
			});
		} catch (RuntimeException re) {
						logger.error("find all failed", re);
				throw re;
		}
	}
	

	public static IEventRequestDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (IEventRequestDAO) ctx.getBean("EventRequestDAO");
	}
}